const { values } = require("../../lib/runtime/runtime")
import { Api} from '../../config/api.js';
import {
    request,
    WXshowToast,
    WXNavigateTo
} from '../../utils/index.js';
Page({


  data: {
      password: "",
  },


  onLoad: function (options) {
      this.giftCart()
      this.setData({
          goods_id:options.goodid,
          price:options.price,
          address_id: options.address_id
      })
  },

   handelpass(e){
       let value = e.detail.value
       this.setData({
           password: value
       })   
   },

  async handelbtn() {
      const {
          password,
          goods_id,
          price,
          address_id
      } = this.data
    if(!password){
        WXshowToast("请填写兑换卡密码")
        return
    }
      const {data} = await request({
          url: Api.exchange,
           method: "POST",
           data:{
                password,
                goods_id,
                price,
                address_id
           }
      })
      if(data.code==2001){
            WXshowToast("礼品对换成功")
            setTimeout(()=>{
               WXNavigateTo("/pages/index/index")  //兑换成功跳转
            },500)
      } else if (data.code == 1001) {
          WXshowToast(data.msg)
      }else{
          WXshowToast("礼品卡密码错误")
      }
      
   },

   async giftCart(){
         const {data} = await request({
          url: Api.giftCart,
           method: "POST"
      })
      console.log(data)
   }
})